{include file="header/index" /}
<style>
    .total_money {
        width: 55%;
        text-align: right;
        font-size: 20px;
        padding: 10px 5px;
        box-sizing: border-box;
    }

    .total_money b {
        margin: 0 5px;
        color: #ff5722;
    }
</style>
<div class="newContentBox">
    <!-- 分割部分 -->
    <!--    <div class="newDivision"></div>-->
    <div class="newListPart">
<!--        <div class="newTitleBox">-->
<!--            <span class="layui-inline newListTitle">业绩划拨列表</span>-->
<!--        </div>-->
        <form method="get" name="searchuser" action="" class="layui-form">
            <div class="newForm">
                <div class="newGroup">
                    <div class="newGroupInput">
                        <input type="text" id="dateTime" placeholder="请选择时间范围" readonly value="" class="layui-input cover_layui_i newDate">
                    </div>
                </div>
                <div class="newGroup">
                    <div class="newGroupInput">
                        <input type="text" id="contract_number" value="" placeholder="请输入合同编号" class="layui-input cover_layui_i">
                    </div>
                </div>

                <div class="newGroup newBtnBox">
                    <a id="search" class="newBtnDefault newBtnPrimary">搜索</a>
                </div>

                <div class="total_money">
                    总金额：<b id="total_money"></b>元
                </div>
                {if condition="isset($tempMoneyTransfer) && $tempMoneyTransfer"}
                <div class="rightBtnBox" id="transfer">

                </div>
                {/if}
            </div>
        </form>
        <div class="newTableBox">

            <table class="layui-table">
                <thead>
                <tr>
                    <th>合同编号</th>
                    <th>客户</th>
                    <th>员工</th>
                    <th>角色</th>
                    <th>结算金额/元</th>
                    <th>结算时间</th>
                </tr>
                </thead>
                <!--套数据 laypage-->
                <script id="list-template" type="text/html">

                    {{# for(var i=0;i<d.length;i++){  }}
                    <tr>
                            <input type="hidden" name="id[]" value="{{d[i].id}}">
                        <td>
                            {{# if(d[i].achievement_work_order_type==3){ }}
                            [新]
                            {{# }else{ }}
                            [续]
                            {{# } }}
                            {{d[i].contract_number}}
                        </td>
                        <td>{{d[i].username?d[i].username:'--'}}</td>
                        <td>{{d[i].workers_name}}<!--[{{d[i].workers_number}}]--></td>
                        <td>
                            {{# if(d[i].workers_position==1){ }}
                            业务员
                            {{# }else if(d[i].workers_position==2){ }}
                            区域合伙人
                            {{# }else if(d[i].workers_position==3){ }}
                            城市合伙人
                            {{# }else if(d[i].workers_position==4){ }}
                            渠道商
                            {{# }else if(d[i].workers_position==5){ }}
                            渠道业务员
                            {{# }else if(d[i].workers_position==6){ }}
                            工程师傅
                            {{# }else if(d[i].workers_position==7){ }}
                            总账号
                            {{# }else{ }}
                            未知
                            {{# } }}
                        </td>
                        <td>{{d[i].money}}</td>
                        <td>{{d[i].add_time}}</td>
                    </tr>
                    {{# } }}
                </script>
                <tbody id="list-content"></tbody>
            </table>
            <!--<a class="newBtnDefault newBtnPrimary" onclick="exportRelation()"> 导出当前结算明细 </a>-->
            <!--用来存当前页码数-->
            <input type="hidden" value="1" id="curr_" />
            <div class="laypage">
                <div class="laypage_total">共<span id="total_number"></span>条数据&nbsp;&nbsp;<span id="allpage"></span></div>
                <div class="laypage_skip" id="AjaxPage"></div>
            </div>

        </div>

    </div>

</div>
<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
    var layer = layui.layer,
        element = layui.element,
        laydate = layui.laydate,
        laypage = layui.laypage,
        form = layui.form,
        laytpl = layui.laytpl;
    $.ajaxSetup({headers: {token: $.cookie('token'),company: $.cookie('company_id')}});
    //日期时间范围选择
    $('#dateTime').each(function(){
        laydate.render({
            elem: this,
            range: '~'
        })
    });

    /*// 导出当前关系
    function exportRelation() {
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        var request_data = {};
        request_data.contract_number = $("#contract_number").val();
        request_data.c_time = $("#dateTime").val();
        request_data = JSON.stringify(request_data);
        $.post("{:config('api')}/Achievement/Dealer/exportDealerSettlement", request_data, function(data){
            layer.close(index);
            if (data.code > 0){
                layer.msg(data.message,{icon:1,time:1500,shade: 0.1,});
                window.location.href = "{:config('api')}/" + data.data.url;
                return false;
            }
            layer.msg(data.message,{icon:2,time:1500,shade: 0.1,});
        });
    }
*/
    Ajaxpage();
    function Ajaxpage(curr){
        var loadingFlag= layer.msg('正在读取数据，请稍候……', { icon: 16, shade: 0.01,shadeClose:false,time:60000 });
        $("#curr_").val(curr);
        var request_data = {};
        request_data.c_time = $("#dateTime").val();
        request_data.contract_number = $("#contract_number").val();
        request_data.page = curr || 1;
        request_data = JSON.stringify(request_data);
        $.post("{:config('api')}/Achievement/Dealer/transfer", request_data, function(data){
            layer.close(loadingFlag);
            var page = data.data.page;
            $("#total_number").text(page.total);
            $("#total_money").text(data.data.total_money);
            if(data.data.total_money>0){
                $("#transfer").html('<a onclick="tempMoneyTransfer()" class="newBtnDefault">划拨</a>');
            }

            data = data.data.data;
            if(data==''){
                $("#list-content").html('<td colspan="20" style="padding-top:10px;padding-bottom:10px;font-size:16px;text-align:center">暂无数据</td>');
            }else{
                // var cache = $.cookie('tipsSettle');
                // if (cache !== 'true') {
                //     setTimeout(function () {
                //         top.layer.msg('工程师傅讲解不在这里展示', {icon: 7, time: 3000});
                //     },1000);
                //     $.cookie('tipsSettle', true);
                // }
                var tpl = document.getElementById('list-template').innerHTML;
                laytpl(tpl).render(data, function(html){
                    document.getElementById('list-content').innerHTML = html;
                });

            }
            //分页
            laypage.render({
                elem: 'AjaxPage' //分页容器的id
                ,count: page.total //总条数
                ,limit: page.row //每页条数
                ,skin: '#1AB5B7' //自定义选中色值
                ,curr: curr || 1
                ,layout: [ 'prev', 'page', 'next', 'skip']
                //,skip: true //开启跳页
                ,jump: function(obj, first){
                    if(!first){
                        Ajaxpage(obj.curr)
                    }
                    $('#allpage').html('第'+ obj.curr +'页，共'+ page.total_page +'页');
                }
            });
        });
    }
    // 搜索表单
    $("#search").click(function () {
        Ajaxpage();
    });
    //跳转详情
    function jump(id){
        window.location.href="{:url('Achievement/achdetails')}?id="+id;
    }

    /**
     * 手动划拨
     */
    function tempMoneyTransfer() {
        var money=$("#total_money").text();

        var data = {};
        data.c_time = $("#dateTime").val();
        data.money=money;
        data.contract_number = $("#contract_number").val();
        data = JSON.stringify(data);
        layer.open({
            content: '确认划拨'+money+'元？'
            ,btn: ['确认', '取消']
            ,yes: function(index, layero){
                //按钮【按钮一】的回调
                $.post("{:config('api')}/Achievement/Dealer/dotransfer", data,function(res){
                    parent.layer.close(index);
                    if (res.code > 0){
                        parent.layer.msg(res.message,{icon:1,time:1500,shade: 0.1,});
                        window.location.href = "{:url('Performance/transfer')}";
                        return false;
                    }
                    parent.layer.msg(res.message,{icon:2,time:1500,shade: 0.1,});
                });
            }
            ,btn2: function(index, layero){
                //按钮【按钮二】的回调

                //return false 开启该代码可禁止点击该按钮关闭
            }
            ,cancel: function(){
                //右上角关闭回调
                //return false 开启该代码可禁止点击该按钮关闭
            }

        });


    }


</script>
